package com.mzy.linear_struct.ks

/**
 * @Author: mizy3
 * @Date: 2024/08/24/下午7:09
 * @Description:
 */
class 路径总和 {
    class TreeNode(var `val`: Int) {
        var left: TreeNode? = null
        var right: TreeNode? = null

        constructor(`val`: Int, left: TreeNode, right: TreeNode) : this(`val`) {
            this.left = left
            this.right = right
        }
    }

    fun hasPathSum(root: TreeNode?, targetSum: Int): Boolean {
        if(root == null) return false
        var count : Int =  targetSum - root.`val`
        if(root.left == null && root.right == null && count == 0){
            return true
        }
        if(root.left == null && root.right == null){
            return false
        }

        if(root.left != null){
            if(hasPathSum(root.left, count)){
                return true;
            }
        }
        if(root.right != null){
            if(hasPathSum(root.right, count)){
                return true
            }
        }

        return false
    }
}